unit uThreadSync;

interface

uses IdSync ,IdThread, IdComponent, uTypes, SysUtils ;

type
  Operations = (OpIncApiUsage,
                OpOnWork,
                OpOnSave,
                OpReturnResult,
                OpReturnDResult,
                OpUpdateDThreadParams,
                OpSaveItemDetails,
                OpUpdateItemDetails,
                OpUpdTimeShift,
                OpSaveAspect,
                OpClearAspect,
                OpSavePictureDetails,
                OpSaveCategory,
                OpUpdateTotalItems
                );

  TFTSync = class(TidSync)
  public
    Value: String;
    iVal : Integer;
    iReal : Real;
    EbayTime : TDateTime;
    XRec : TSearchThreadStatus;
    DRec : TDownloadImageThreadStatus;
    FItemRec: TItemDetails;
    FAspectArray : TAspectArray;
    FAspectName : string;
    FSearchID : integer;
    FThread : TIdThread;
    WorkMode: TWorkMode;
    Operation : Operations;
    procedure DoSynchronize; override;
    procedure IncApiUsage;
    procedure OnHttpWork(AWorkMode: TWorkMode; const AWorkCount: Integer);
    procedure OnSave(Saved,Total : Integer);
    procedure UpdThreadStatus(FRec : TSearchThreadStatus; SearchID : Integer);
    procedure UpdDThreadStatus(FRec : TDownloadImageThreadStatus);
    procedure SaveItemDetails(ItemRec : TItemDetails);
    procedure UpdateItemDetails(ItemDetails: TItemDetails);
    procedure SavePictureDetails(ItemRec: TItemDetails);
    procedure SaveAspect(SearchID: Integer; AspectName: String; AspectArray : TAspectArray);
    procedure ClearAspect(SearchID: Integer);
    procedure UpdTimeShift(tEbayTime : string);
    procedure UpdDThreadParams;
    procedure UpdateTotalItems(TotalItems: integer);
    procedure SaveCategory;
  end;

implementation

uses uEbaySearchThread, uMain, functions, uLog, uGetCategories, uSearches;

{ TFTSync }
procedure TFTSync.DoSynchronize;
var x : Integer;
    i : Integer;
begin
  inherited;
  case Operation of
    OpIncApiUsage : fmMain.IncApiUsage;
    OpOnWork : begin
      case WorkMode of
        wmRead : begin
          fmMain.RxCounter := fmMain.RxCounter + iVal;
          fmMain.RxCounterDelta := fmMain.RxCounterDelta + iVal;
        end;
        wmWrite : begin
          fmMain.TxCounter := fmMain.TxCounter + iVal;
          fmMain.TxCounterDelta := fmMain.TxCounterDelta + iVal;
        end
      end;
    end;
    OpOnSave : with fmMain do begin
      sbrStatusBar.Panels[4].Text := Value;
    end;
    OpReturnResult : with fmMain do
      for i:=0 to High(SearchStatusArr) do
        if SearchStatusArr[i].ThreadID = iVal then begin
          SearchStatusArr[i].TotalPages := XRec.TotalPages;
          SearchStatusArr[i].LoadedPages := XRec.LoadedPages;
          SearchStatusArr[i].TotalItems := XRec.TotalItems;
          SearchStatusArr[i].LoadedItems := SearchStatusArr[i].LoadedItems + XRec.LoadedItems;
          SearchStatusArr[i].SavedItems := SearchStatusArr[i].SavedItems + XRec.SavedItems;
          SearchStatusArr[i].CurrentCategory := XRec.CurrentCategory;
          SearchStatusArr[i].TotalCategories := XRec.TotalCategories;
          SearchStatusArr[i].ThreadID := XRec.ThreadID;
          UpdateThreadStatus;
          Break;
        end;
    OpSaveItemDetails : with fmMain do begin
      SaveItemdetails(FItemRec);
    end;
    OpUpdateItemDetails : with fmMain do begin
      SaveUpdateItemDetails(FItemRec);
    end;
    OpUpdTimeShift : with fmMain do begin
      TimeShift := Now - EbayTime;
      TimeShiftSet := True;
      sbrStatusBar.Panels[3].Text := 'Time shift: '+TimeToStr(TimeShift);
      inisettings.timeshiftebay := TimeShift;
    end;
    OpSaveAspect : with fmMain do begin
      fmMain.SaveAspect(FSearchID,FAspectName,FAspectArray);
    end;
    OpClearAspect : with fmMain do begin
      fmMain.ClearAspect(FSearchID);
    end;
    OpSavePictureDetails : with fmMain do begin
      SavePicturedetails(FItemRec);
    end;
    OpSaveCategory : fmMain.SaveCategory((FThread as TGetCategoriesThread).FCategory);
    OpUpdateTotalItems : if fmSearches <> nil then fmSearches.lbl27.Caption := IntToStr(iVal);
  end;
end;


procedure TFTSync.IncApiUsage;
begin
  Operation := OpIncApiUsage;
  Synchronize;
end;

procedure TFTSync.OnHttpWork(AWorkMode: TWorkMode; const AWorkCount: Integer);
begin
  WorkMode := AWorkMode;
  iVal := AWorkCount;
  Operation := OpOnWork;
  Synchronize;
end;

procedure TFTSync.OnSave(Saved, Total: Integer);
begin
  Operation := OpOnSave;
  Value := Format('Saved %d of %d items',[Saved,Total]);
  Synchronize;
end;

procedure TFTSync.UpdThreadStatus(FRec : TSearchThreadStatus; SearchID : Integer);
begin
  iVal := SearchID;
  Operation := OpReturnResult ;
  XRec := FRec;
  Synchronize;
end;

procedure TFTSync.UpdDThreadStatus(FRec : TDownloadImageThreadStatus);
begin
  Operation := OpReturnDResult ;
  DRec := FRec;
  Synchronize;
end;

procedure TFTSync.UpdDThreadParams;
begin
  Operation := OpUpdateDThreadParams ;
  Synchronize;
end;

procedure TFTSync.SaveItemDetails(ItemRec: TItemDetails);
begin
  Operation := OpSaveItemDetails;
  FItemRec:= ItemRec;
  Synchronize;
end;

procedure TFTSync.SavePictureDetails(ItemRec: TItemDetails);
begin
  Operation := OpSavePictureDetails;
  FItemRec:= ItemRec;
  Synchronize;
end;

procedure TFTSync.SaveAspect(SearchID: Integer; AspectName: String; AspectArray : TAspectArray);
begin
  Operation := OpSaveAspect;
  FAspectName := AspectName;
  FSearchID := SearchID;
  FAspectArray:= AspectArray;
  Synchronize;
end;

procedure TFTSync.SaveCategory;
begin
  Operation := OpSaveCategory;
  Synchronize;
end;

procedure TFTSync.ClearAspect(SearchID: Integer);
begin
  Operation := OpClearAspect;
  FSearchID := SearchID;
  Synchronize;
end;

procedure TFTSync.UpdTimeShift(tEbayTime : string);
begin
  Operation := OpUpdTimeShift;
  EbayTime := eBayTimeStrConvert(tEbayTime);
  Synchronize;
end;

procedure TFTSync.UpdateItemDetails(ItemDetails: TItemDetails);
begin
  Operation := OpUpdateItemDetails;
  FItemRec:= ItemDetails;
  Synchronize;
end;

procedure TFTSync.UpdateTotalItems(TotalItems: integer);
begin
  Operation := OpUpdateTotalItems;
  iVal:= TotalItems;
  Synchronize;
end;

end.
